package pqt;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Font;
import java.sql.SQLException;

public class ConsultarBalanzas extends JDialog {

	//private final JPanel contentPanel = new JPanel();
	JTable tablaBalanza;
	JButton btnAceptar = new JButton("Aceptar");
	BaseDeDatos db = BaseDeDatos.getInstance();
	
	public ConsultarBalanzas(final JPanel contentPanel) {
		
		//setBounds(100, 100, 1000, 800);
		//getContentPane().setLayout(new BorderLayout());
		//contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		//getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		final JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(new LineBorder(new Color(51, 153, 255)), "Balanzas ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds((contentPanel.getWidth()/2)-600, 50, 1200, 500);
		contentPanel.add(panel);
		panel.setLayout(null);
		tablaBalanza = new JTable(10,7);
		
				
		tablaBalanza.getTableHeader().setReorderingAllowed(false);
		tablaBalanza.setFont(new Font("Tahoma", Font.PLAIN, 12));
		MyModel m = new MyModel(null, new String[] {
				"Modelo", "Descripcion", "Codigo", "e1", "e2","Carga Maxima", "Caga Media", "Carga Minima","Calibracion","Habilitada"
			});
		tablaBalanza.setModel(m);
		
		
		DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer();
		rightRenderer.setHorizontalAlignment(SwingConstants.CENTER);
		for(int b=0;b<10;b++) {
			tablaBalanza.getColumnModel().getColumn(b).setCellRenderer(rightRenderer);
			
		}
		
		JScrollPane scrollPane = new JScrollPane(tablaBalanza);
		scrollPane.setBounds(10, 23, 1180, 466);
		panel.add(scrollPane);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.setBounds((panel.getX()+panel.getWidth())-89, (panel.getY()+panel.getHeight())+10, 89, 23);
		contentPanel.add(btnCancelar);
		
		
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				contentPanel.getParent().remove(contentPanel);
				
			}
		});
		btnAceptar.setBounds(btnCancelar.getX()-100, btnCancelar.getY(), 89, 23);
		contentPanel.add(btnAceptar);
		consultarBalanzas();
	}
	
	public void consultarBalanzas() {
		
		Balanza balanza[];
		
		try {
			
			balanza = db.consultarBalanzas(true);
			
			System.out.println("lennn:"+balanza.length);
				
			for(int i=0;i<balanza.length;i++) {
					agregarFila(balanza[i]);
			}
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	public void agregarFila(Balanza balanza) {
		DefaultTableModel model = (DefaultTableModel) tablaBalanza.getModel();
		model.addRow(new Object[]{	balanza.getModelo(), balanza.getDescripcion(), balanza.getCodigo(), 
									balanza.getE1(), balanza.getE2(),balanza.getCargaMaxima(), balanza.getCargaLimite(), balanza.getCargaMinima(),
									balanza.getCalibracion(),balanza.ActivaToString()});
		
		resizeColumnWidth(tablaBalanza);
	}
	
	private void resizeColumnWidth(JTable table) {
	    final TableColumnModel columnModel = table.getColumnModel();
	    for (int column = 0; column < table.getColumnCount(); column++) {
	        int width = 10; // Min width
	        for (int row = 0; row < table.getRowCount(); row++) {
	            TableCellRenderer renderer = table.getCellRenderer(row, column);
	            Component comp = table.prepareRenderer(renderer, row, column);
	            width = Math.max(comp.getPreferredSize().width, width);
	        }
	        columnModel.getColumn(column).setPreferredWidth(width);
	    }
	}
	
	public class MyModel extends DefaultTableModel 
	{
	   public MyModel(Object[][] data, Object[] cols) 
	   {
	      super(data, cols);
	   }

	   public boolean isCellEditable(int row, int col) 
	   {
	      return false;
	   }
	}

}

